<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" ref="form" class="demo-form-inline">
        <el-form-item label="订单号">
          <el-input
            v-model="form.order_id"
            placeholder="请输入订单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户编码">
          <el-input
            v-model="form.user_id"
            placeholder="请输入用户编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="modality">
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="text item">
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column prop="order_id" align="center" label="订单号">
        </el-table-column>
        <!-- <el-table-column prop="order_type" align="center" label="订单类型">
        </el-table-column> -->
        <el-table-column prop="order_type" align="center" label="商品名称">
          <template slot-scope="scope">
            <span>{{ scope.row.index.items[0].name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="order_type" align="center" label="商品数量">
          <template slot-scope="scope">
            <span>{{ scope.row.index.items[0].nums }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ctime" align="center" label="下单时间">
        </el-table-column>
        <el-table-column prop="status_text" align="center" label="订单状态">
        </el-table-column>
        <el-table-column prop="nickname" align="center" label="用户名称">
        </el-table-column>
        <el-table-column prop="user_id" align="center" label="用户编码">
        </el-table-column>
        <el-table-column prop="ship_name" align="center" label="收货人姓名">
        </el-table-column>
        <el-table-column prop="ship_mobile" align="center" label="收货人手机号">
        </el-table-column>
        <el-table-column
          prop="shouhuoAddress"
          align="center"
          show-overflow-tooltip
          label="收货地址"
        >
        </el-table-column>
        <el-table-column prop="pay_status_text" align="center" label="支付状态">
        </el-table-column>
        <el-table-column
          prop="ship_status_text"
          align="center"
          label="发货状态"
        >
        </el-table-column>
        <el-table-column prop="payed" align="center" label="订单总额">
        </el-table-column>
        <el-table-column label="操作" align="center" width="270  ">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.row)"
              v-if="
                scope.row.pay_status > 1 &&
                scope.row.pay_status != 5 &&
                scope.row.service_status == 1
              "
              >客服接单</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="handle(scope.row)"
              v-if="
                scope.row.pay_status > 1 &&
                scope.row.pay_status != 5 &&
                scope.row.service_status == 2 &&
                scope.row.ship_status == 0
              "
              >客服派单
            </el-button>
            <el-button
              size="mini"
              type="primary"
              v-if="scope.row.pay_status > 1 && scope.row.ship_status == 1"
              >未接单
            </el-button>
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pay_status > 1 && scope.row.ship_status == 2"
              >已发货
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              v-if="scope.row.status == 1 && scope.row.pay_status == 1"
              >取消订单
            </el-button>
            <el-button
              size="mini"
              type="info"
              v-if="scope.row.status == 3 && scope.row.pay_status == 1"
              >已取消
            </el-button>
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pay_status > 1 && scope.row.ship_status == 3"
              >已完成
            </el-button>
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pay_status == 5 && scope.row.ship_status != 3"
              >已退款
            </el-button>
            <!-- <el-button size="mini" type="primary" @click="cancel(scope.row)">退款</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </el-card>
</template>

<script>
import { orderList, takeOrders, delivery, quxiao } from "../../../utils/api.js";
export default {
  data() {
    return {
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      order_type: 5,
      // 收集查询参数
      form: {
        order_id: "", //订单号
        phoneNumber: "",
        name: "",
        time: "",
        user_id: "",
        order_id: "",
        order_sh_name: "",
        status: "",
        pay_status: "",
        ship_status: "",
        order_time: "",
      },
      imgHeader: this.Global.imgHeader,
      time: "",
      //展示数据
      tableData: [
        {
          order_id: "",
          order_type: "",
          status: "",
          ctime: "",
          nickname: "",
          user_mobile: "",
          ship_name: "",
          ship_mobile: "",
          shouhuoAddress: "",
          pay_status: "",
          ship_status: "",
          payed: "",
          service_status: "",
        },
      ],
      //分页数据
      page: 1,
      limit: 10,
      total: 0,
    };
  },
  methods: {
    //日期
    timeChange(e) {
      console.log(e);
      let list = [];
      e.forEach((item) => {
        let date = new Date(item);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let h = date.getHours();
        let min = date.getMinutes();
        let s = date.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        h = h < 10 ? "0" + h : h;
        min = min < 10 ? "0" + min : min;
        s = s < 10 ? "0" + s : s;
        list.push(
          year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s
        );
      });

      this.time = list[0] + "," + list[1];
      console.log(this.time);
    },
    //查询
    onSubmit() {
      this.page = 1;
      this.orderLists();
    },
    // 重置
    onReset() {
      this.form.order_id = "";
      this.form.user_id = "";
      this.time = "";
      this.form.time = "";
      this.orderLists();
    },
    //取消订单
    handleDelete(row) {
      this.$confirm("此操作将会取消订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          quxiao(this.token, row.order_id).then((res) => {
            if (res.data.status) {
              this.$message({
                message: "操作成功",
                type: "success",
              });
              this.orderLists();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    //接单
    handleEdit(row) {
      if (row.service_status == 2) {
        this.$message({
          message: "已接单完成,请点击派单",
          type: "warning",
          onClose: (res) => {},
        });
      } else {
        let obj = JSON.parse(JSON.stringify(row));
        this.tableData.order_id = obj.order_id;
        this.takeOrdersS();
      }
    },
    //派单
    handle(row) {
      // 1未接单，2已接单
      if (row.ship_status != 0) {
        this.$message({
          message: "已经派单了",
          type: "warning",
          // onClose: (res) => {},
        });
        return;
      }
      if (row.service_status == 2) {
        let obj = JSON.parse(JSON.stringify(row));
        this.tableData.order_id = obj.order_id;
        this.deliveryS();
      } else {
        this.$message({
          message: "请先接单",
          type: "warning",
          onClose: (res) => {},
        });
      }
    },
    //获取列表数据
    orderLists() {
      this.loading = true;
      orderList(
        this.token,
        this.page,
        this.limit,
        this.form.order_id,
        this.form.status,
        this.form.pay_status,
        this.form.ship_status,
        this.order_type,
        this.form.order_sh_name,
        this.time,
        this.form.user_id
      )
        .then((res) => {
          if (res.data.status) {
            this.tableData = res.data.data.data;
            this.total = res.data.data.count;
            this.tableData.forEach((item) => {
              if (item.avatar) {
                item.avatar = this.imgHeader + item.avatar;
              }
              if (item.order_type == 5) {
                item.order_type = "定制水";
              }
              if (item.status == 1) {
                item.status_text = "正常";
              }
              if (item.status == 2) {
                item.status_text = "完成";
              }
              if (item.status == 3) {
                item.status_text = "取消";
              }
              if (item.pay_status == 1) {
                item.pay_status_text = "未付款";
              }
              if (item.pay_status == 2) {
                item.pay_status_text = "已支付";
              }
              if (item.pay_status == 3) {
                item.pay_status_text = "部分支付";
              }
              if (item.pay_status == 4) {
                item.pay_status_text = "部分退款";
              }
              if (item.pay_status == 5) {
                item.pay_status_text = "已退款";
              }
              // `ship_status` '发货状态 0=未派单 1=未接单 2=已发货 3=已送达
              if (item.ship_status == 0) {
                item.ship_status_text = "未派单";
              }
              if (item.ship_status == 1) {
                item.ship_status_text = "未接单";
              }
              if (item.ship_status == 2) {
                item.ship_status_text = "已发货";
              }
              if (item.ship_status == 3) {
                item.ship_status_text = "已送达";
              }
              // 收货地址
              item.shouhuoAddress = item.ship_address_ds + item.ship_address;
              if (item.ship_address_ds == null && item.ship_address == null) {
                item.shouhuoAddress = "";
              }
            });
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //接单
    takeOrdersS() {
      takeOrders(this.token, this.tableData.order_id, "")
        .then((res) => {
          if (res.data.status) {
            this.$message({
              message: "接单成功",
              type: "success",
              onClose: (res) => {},
            });
            this.orderLists();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    // 派单
    deliveryS() {
      delivery(this.token, this.tableData.order_id)
        .then((res) => {
          if (res.data.status) {
            this.$message({
              message: "派单成功",
              type: "success",
              onClose: (res) => {},
            });
            this.orderLists();
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.orderLists();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.orderLists();
    },
    // cancel(row){
    //   console.log(row)
    // }
  },
  mounted() {
    this.orderLists();
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.modality {
  margin-left: 2%;
}

.length {
  width: 320px;
}
</style>